<template>
    <div class="mod-config">
      <el-table
        :data="dataList"
        border
        v-loading="dataListLoading"
        @selection-change="selectionChangeHandle"
        style="width: 100%;"
      >
        <el-table-column type="selection" header-align="center" align="center" width="50"></el-table-column>
        <el-table-column prop="id" header-align="center" align="center" label="id"></el-table-column>
        <el-table-column prop="spu_name" header-align="center" align="center" label="名称"></el-table-column>
        <el-table-column prop="spu_description" header-align="center" align="center" label="描述"></el-table-column>
        <el-table-column prop="catelog_id" header-align="center" align="center" label="分类"></el-table-column>
        <el-table-column prop="brand_id" header-align="center" align="center" label="品牌"></el-table-column>
        <el-table-column prop="weight" header-align="center" align="center" label="重量"></el-table-column>
        <el-table-column prop="publish_status" header-align="center" align="center" label="上架状态">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.publish_status == 0">新建</el-tag>
            <el-tag v-if="scope.row.publish_status == 1">已上架</el-tag>
            <el-tag v-if="scope.row.publish_status == 2">已下架</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="create_time" header-align="center" align="center" label="创建时间"></el-table-column>
        <el-table-column prop="update_time" header-align="center" align="center" label="修改时间"></el-table-column>
        <el-table-column fixed="right" header-align="center" align="center" width="150" label="操作">
          <template slot-scope="scope">
            <el-button
              v-if="scope.row.publish_status == 0"
              type="text"
              size="small"
              @click="productUp(scope.row.id)"
            >上架</el-button>
            <el-button type="text" size="small" @click="attrUpdateShow(scope.row)">规格</el-button>
          </template>
        </el-table-column>
      </el-table>
  
      <el-pagination
        @size-change="sizeChangeHandle"
        @current-change="currentChangeHandle"
        :current-page="pageIndex"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="pageSize"
        :total="totalPage"
        layout="total, sizes, prev, pager, next, jumper"
      ></el-pagination>
    </div>
  </template>
  
  <script>
  import PubSub from 'pubsub-js';
  export default {
    data() {
      return {
        dataSub: null,
        dataForm: {},//接受传过来的查询条件
        dataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        dataListSelections: [],
        addOrUpdateVisible: false
      };
    },
    props: {
      catId: {
        type: Number,
        default: 0
      }
    },
    components: {},
    activated() {
      this.getDataList();
    },
    methods: {
      productUp(id) {
        this.$http({
          url: this.$http.adornUrl("/product/spu/" + id + "/up"),
          method: "post"
        }).then(({ data }) => {
          if (data && data.code === 200) {
            this.$message({
              message: data.msg,
              type: "success",
              duration: 1500,
              onClose: () => {
                this.getDataList();
              }
            });
          } else {
            this.$message.error(data.msg);
          }
        });
      },
      attrUpdateShow(row) {
        console.log(row);
        this.$router.push({
          path: "/product-attrupdate",
          query: { spuId: row.id, catelogId: row.catelog_id}
        });
      },
      // 获取数据列表
      getDataList() {
        this.dataListLoading = true;
        let data={
            page: this.pageIndex,
            limit: this.pageSize,
            key:this.dataForm.key,
            brandId:this.dataForm.brandId,
            catelogId:this.dataForm.catelogId,
            status:this.dataForm.status
        }
        this.$http({
          url: this.$http.adornUrl("/product/spu/spuinfo/list"),
          method: "post",
          data: this.$http.adornParams(data,false)
        }).then(({ data }) => {
          if (data && data.code === 200) {
            this.dataList = data.data.list;
            this.totalPage = data.data.totalCount;
          } else {
            this.dataList = [];
            this.totalPage = 0;
          }
          this.dataListLoading = false;
        });
      },
      // 每页数
      sizeChangeHandle(val) {
        this.pageSize = val;
        this.pageIndex = 1;
        this.getDataList();
      },
      // 当前页
      currentChangeHandle(val) {
        this.pageIndex = val;
        this.getDataList();
      },
      // 多选
      selectionChangeHandle(val) {
        this.dataListSelections = val;
      },
      // 新增 / 修改
      addOrUpdateHandle(id) {}
    },
    mounted() {
      this.dataSub = PubSub.subscribe("dataForm", (msg, val) => {
        console.log("~~~~~", val);
        this.dataForm = val;
        this.getDataList();
      });
    },
    beforeDestroy() {
      PubSub.unsubscribe(this.dataSub);
    }
  };
  </script>
  